/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPCrediblePatternReceiver.
//
// UDPCrediblePatternReceiver is a simple pattern witch achieves acknowledgement
// packet just like TCP, and this pattern can ensure the packet is ordered.
//

#ifndef __UDP__CREDIBLE__PATTERN__RECEIVER__H
#define __UDP__CREDIBLE__PATTERN__RECEIVER__H

#include "common.h"
#include "sort_queue_dynamic_buffer.h"
#include "serial_number_pool.h"
#include "packet_define.h"

namespace amz {

class UDPCrediblePatternReceiver {
 public:
  UDPCrediblePatternReceiver(uint32 max_serial_number);
  virtual ~UDPCrediblePatternReceiver();

  bool CheckAcknowledgePacket(const char *udp_packet) const { return CHECK_CREDIBLE_UDP_ACK(*(uint32 *)udp_packet); }

  // Get head.
  uint32 PickHead(const char *udp_packet) {
    return *(uint32 *)udp_packet;
  }

  // Pick up serial number from a udp packet.
  uint32 PickSerialNumber(const char *udp_packet);
  // Packet a UDP ack packet. This packet should be after write into the receive buffer.
  // even_flag: is even order.
  // return the length of packet.
  // = 0: not enough space.
  // < 0: error.
  int PacketAcknowledge(bool even_flag, uint32 serial_number, char *packet, int max_size);
  // Check the serial number is the right ordering.
  bool CheckSerialNumberOrder(uint32 serial_number);

  // Parity cycle time contrast
  bool CheckParityCycleTimeContrast(uint32 serial_number, bool even_flag);

  // Insert into the UDP waiting for receiving queue, and mark arriving in the
  // serial_number_pool_. packet is not a unpacket data.
  bool InsertSortQueue(uint32 serial_number, const char *packet, int size);

  // Receive packet data(Unpacket data) from sort_queue_.
  // This method will check current serial number is arrive in the sort_queue_.
  // Loop this until return the value <= 0.
  int ReceiveFromSortQueue(char *data, int max_size);

  // Unpacket a UDP packet.
  int Unpacket(const char *packet, int size, char *data, int max_size);

 protected:
  // Count queue, use to add a serial number to packet
  SerialNumberPool<bool> serial_number_pool_;
  // the queue to wait for acknowledge packet.
  SortQueueBufferDynamic<bool> sort_queue_;
};

}  // namespace amz

#endif  // __UDP__CREDIBLE__PATTERN__RECEIVER__H
